<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\CompanyHonorDisgraceFormat;
use App\Http\Requests\SetCompanyHonorDisgraceRequest;
use App\Services\CompanyHonorDisgraceService;
use Illuminate\Http\Request;

class CompanyHonorDisgraceController extends Controller
{
    protected $companyHonorDisgraceService;

    public function __construct(CompanyHonorDisgraceService $companyHonorDisgraceService)
    {
        $this->companyHonorDisgraceService = $companyHonorDisgraceService;
    }

    public function create(SetCompanyHonorDisgraceRequest $setCompanyHonorDisgraceRequest)
    {
        $format = new CompanyHonorDisgraceFormat($setCompanyHonorDisgraceRequest->all());
        $this->companyHonorDisgraceService->setCompanyHonorDisgrace($format);
        return renderSuccessJson();
    }

    public function update(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数');
        }
        $format = new CompanyHonorDisgraceFormat($request->all());
        $this->companyHonorDisgraceService->setCompanyHonorDisgrace($format);
        return renderSuccessJson();
    }

    public function getList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('type')) {
            $search['type'] = $request->input('type');
        }
        if ($request->input('company_name')) {
            $search['company_name'] = $request->input('company_name');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }

        $res = $this->companyHonorDisgraceService->getList($page, $pageSize, $search);

        return renderSuccessJson($res);
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        $res = $this->companyHonorDisgraceService->detail($id);
        return renderSuccessJson($res);
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $this->companyHonorDisgraceService->delete($id);
        return renderSuccessJson();
    }
}
